package com.atguigu.config;

import com.atguigu.constant.MqConst;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class CancelOrderCustomer {
    //注入队列
    @Bean
    public Queue cancelOrderQueue(){
        return new Queue(MqConst.CANCEL_ORDER_QUEUE,false);
    }
    //注入交换机  由于是延迟队列 所以交换机用自定义交换机
    @Bean
    public CustomExchange cancelOrderExchange(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type", "direct");//自定义交换机的类型
        return new CustomExchange(MqConst.CANCEL_ORDER_EXCHANGE,
                "x-delayed-message",false,false,arguments);
    }
    @Bean//绑定交换机和队列 以及 路由key
    public Binding bindingQueueAndExchange(@Qualifier("cancelOrderQueue") Queue cancelOrderQueue,
                                           @Qualifier("cancelOrderExchange") CustomExchange cancelOrderExchange){

        return BindingBuilder.bind(cancelOrderQueue).to(cancelOrderExchange).with(MqConst.CANCEL_ORDER_ROUTE_KEY).noargs();

    }
}
